package com.cgbl.cg.vm.mine

import android.app.Application
import androidx.databinding.ObservableInt
import api.ProjectServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.CustomerItemInfo
import com.robot.base.model.PageListBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class ProjectCenterViewModel(application: Application) : GLBaseViewModel(application) {

    val tabType = ObservableInt(TAB_ALL)

    var pageNo = 1

    val uc = UiChangeEvent()

    class UiChangeEvent {
        val dataEvent = SingleLiveEvent<List<CustomerItemInfo>>()
        val refreshEvent = SingleLiveEvent<Int>()
    }

    val clickSelectAll = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_ALL)
    })

    val clickSelectPending = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_PENDING)
    })

    val clickSelectDevelop = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_DEVELOP)
    })

    val clickSelectClose = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_CLOSE)
    })

    val clickSelectComplete = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_COMPLETE)
    })

    fun refresh() {
        pageNo = 1
        getCustomerItemInfo()
    }

    fun load() {
        pageNo++
        getCustomerItemInfo()
    }

    fun getCustomerItemInfo() {
        val map = hashMapOf(
            "pageNo" to pageNo,
            "pageSize" to 10,
        )
        when (tabType.get()) {
            TAB_PENDING -> {
                map["status"] = 0
            }
            TAB_DEVELOP -> {
                map["status"] = 3
            }
            TAB_CLOSE -> {
                map["status"] = 5
            }
            TAB_COMPLETE -> {
                map["status"] = 4
            }
        }
        ProjectServiceFactory.getCustomerItemInfo(
            map as Map<String, Any>?
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<CustomerItemInfo>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<CustomerItemInfo>>) {
                uc.dataEvent.postValue(t.data.list)
                uc.refreshEvent.postValue(
                    if (t.data.total.toInt() < pageNo * 10) {
                        0
                    } else {
                        1
                    }
                )
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(
                    0
                )
            }
        })
    }

    companion object {

        const val TAB_ALL = 1

        const val TAB_PENDING = 2

        const val TAB_DEVELOP = 3

        const val TAB_CLOSE = 4

        const val TAB_COMPLETE = 5
    }


}